package com.bright.servlet;

import com.alibaba.fastjson.JSON;
import com.bright.entity.Oss;
import com.bright.service.Impl.OssServiceImpl;
import com.bright.service.OssService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/oss")
public class OssServlet extends HttpServlet {
    OssService ossService = new OssServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String opr = request.getParameter("opr");
        switch (opr){
            case "selectAllOss":
                selectAllOss(request,response);
                break;
            case "selectByOssName":
                selectByOssName(request,response);
                break;
            case "add":
                add(request,response);
                break;
            case "updatePermission":
                updatePermission(request,response);
                break;
        }
    }
//更改数据
    private void updatePermission(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setCharacterEncoding("utf-8");
        String bucketName = request.getParameter("bucketName");
        String permission = request.getParameter("permission");
        boolean updateOss = ossService.update(bucketName,permission);
        String s = JSON.toJSONString(updateOss);
        response.getWriter().write(s);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        doPost(request,response);
    }
//查询所有数据
    protected void selectAllOss(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setCharacterEncoding("utf-8");
        List<Oss> ossList = ossService.selectAll();
        String s = JSON.toJSONString(ossList);
        response.getWriter().write(s);
    }
//根据名字查询数据
    protected void selectByOssName(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setCharacterEncoding("utf-8");
        String bucketName = request.getParameter("bucketName");
        Oss oss = ossService.selectByName(bucketName);
        String s = JSON.toJSONString(oss);
        response.getWriter().write(s);
    }
//新增数据
    protected void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setCharacterEncoding("utf-8");
        String bucketName = request.getParameter("bucketName");
        String ossType = request.getParameter("ossType");
        String ossMission = request.getParameter("ossMission");
        Oss oss = new Oss();
        oss.setPermission(ossMission);
        oss.setType(ossType);
        oss.setBucketName(bucketName);
        boolean insertOss = ossService.insert(oss);
        String s = JSON.toJSONString(insertOss);
        response.getWriter().write(s);
    }
}
